package com.apsfc.controller;

import com.apsfc.po.Notice;
import com.apsfc.po.Type;
import com.apsfc.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 灯灯
 */
@Controller
@RequestMapping("/admin")
public class NoticeController {
    @Autowired
    @Qualifier("noticeService")
    private NoticeService noticeService;

    @RequestMapping("/getAllNotice")
    public String getAllNotice(Model model){
        List<Notice> notices = noticeService.getAllNotice();
        model.addAttribute(notices);
        System.out.println(notices);
        return "admin/notice";
    }

    @RequestMapping(value="/NoticeAdd",method = RequestMethod.POST)
    public String NoticeAdd(String name, String content){
        if(name.equals("") || content.equals("")){
            return "admin/notice_add";
        }else {
            Notice notice = new Notice();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            notice.setName(name);
            notice.setContent(content);
            notice.setTimes(df.format(new Date()));
            noticeService.NoticeAdd(notice);
            return "admin/notice_add";
        }
    }

    @RequestMapping("/toNoticeUpdate/{id}")
    public ModelAndView toNoticeUpdate(@PathVariable("id") int id){
        Notice notice = noticeService.findNoticeById(id);
        return new ModelAndView("/admin/notice_update","notice", notice);
    }

    @RequestMapping("/NoticeUpdate")
    public ModelAndView NoticeUpdate(Notice notice){
        Notice notice1 = notice;
        if(notice.getName().equals("")){
            return new ModelAndView("/admin/notice_update","notice", notice);
        }else {
            noticeService.NoticeUpdate(notice);
            List<Notice> notices = noticeService.getAllNotice();
            return new ModelAndView("admin/notice","notices", notices);
        }
    }
}
